package com.woan.api.services;

import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;

import com.woan.api.util.IphonePush;
import com.woan.api.util.DateJsonValueProcessor;
import com.woan.api.vo.VadmininfoList;
import com.woan.api.vo.VmessageList;
import com.woan.api.idao.SysAdmininfoIDAO;
import com.woan.api.idao.SysMessageIDAO;
import com.woan.api.idao.ZbControlIDAO;
import com.woan.api.idao.dao.SysAdmininfoMobileDAO;
import com.woan.api.model.SysAdmininfo;
import com.woan.api.model.SysAdmininfoMobile;
import com.woan.api.model.SysMessage;
import com.woan.api.model.ZbControl;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;



public class ZbcontrolServerServices {
	private SysAdmininfoIDAO sysadmininfoiDAO;
	private ZbControlIDAO zbcontroliDAO;
	private SysAdmininfoMobileDAO sysadmininfomobileiDAO;
	
	public SysAdmininfoMobileDAO getSysadmininfomobileiDAO() {
		return sysadmininfomobileiDAO;
	}
	public void setSysadmininfomobileiDAO(
			SysAdmininfoMobileDAO sysadmininfomobileiDAO) {
		this.sysadmininfomobileiDAO = sysadmininfomobileiDAO;
	}
	public SysAdmininfoIDAO getSysadmininfoiDAO() {
		return sysadmininfoiDAO;
	}
	public void setSysadmininfoiDAO(SysAdmininfoIDAO sysadmininfoiDAO) {
		this.sysadmininfoiDAO = sysadmininfoiDAO;
	}


	public ZbControlIDAO getZbcontroliDAO() {
		return zbcontroliDAO;
	}
	public void setZbcontroliDAO(ZbControlIDAO zbcontroliDAO) {
		this.zbcontroliDAO = zbcontroliDAO;
	}
	/*
	 * 获取本组历史消息
	 */
	public List<ZbControl> listVideoTalkMessage(String videoid){
		List<ZbControl> message = null;
		message = zbcontroliDAO.FindVideoTalkMessageByVideoID(videoid);
		return message;
	}
	public JSONObject sendMessage(HttpServletRequest request,String groupname,String username,String userpwd,ZbControl zbcontrol,String touser){
		boolean flag = false;
		SysAdmininfo user = null;
		user = sysadmininfoiDAO.FindUserByGroupNameAndUsername(username,touser);//根据组名，接受人的用户名查找用户信息。
		//System.out.println(user);
		SysAdmininfoMobile mobile = sysadmininfomobileiDAO.FindAdminMobileByUserid(user.getUid());//查找接收信息人的设备信息
		ResourceBundle rb_config = ResourceBundle.getBundle("config");
		String speaker_text=rb_config.getString("speaker_text");
		String random_seed=rb_config.getString("random_seed");
		String hash_key=rb_config.getString("hash_key");
		flag = zbcontroliDAO.SaveMessage(zbcontrol);
		JSONObject json=new JSONObject();
		if(flag)
		{
			String to_user_name_really = username+"|"+touser+"|"+mobile.getDeviceNum();
			String fromuser = username;
			String urrStr = "";
			urrStr=speaker_text+"?dst_username="+to_user_name_really+"&from_username="+fromuser+"&message="+zbcontrol.getContent()+"&stream_id="+zbcontrol.getVideoId()+"&hash_key="+hash_key+"&random_seed="+random_seed;
			URL url = null;
			InputStream is = null;
			System.out.println(urrStr);
			try 
			{
				url = new URL(urrStr);
				is = url.openStream();
				
			} 
			catch (Exception ex)
			{
				ex.printStackTrace();
			} 
			finally 
			{
				try 
				{
					if (is != null)
						is.close();
				} 
				catch (Exception e) 
				{
					e.printStackTrace();
				}
			}
			json.accumulate("results", 1);
			json.accumulate("errMsg", "发送成功");
		}
		else
		{
			json.accumulate("results", 0);
			json.accumulate("errMsg", "发送失败");
		}
		return json;
	}
}
